import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.management.NotificationListener;
import javax.swing.*;

import java.awt.event.ActionEvent;

public class GamePanel extends JPanel {
	
	// Don't change
	private static final long serialVersionUID = 1877531297571647620L;
	
	NotificationListener NL;
	Graphics2D g2d;
	Board _board;
	int rows;
	int cols;
	boolean BoardCreated = false;
	int SpotTakenCount = 0;
	int FlipEnabled;
	int RemoveEnabled;
	
	JButton FlipButton	= new JButton("Flip Pieces");
	
	
	public GamePanel(int a[], NotificationListener nl){
		NL = nl;
		rows = a[0];
		cols = a[1];
		FlipEnabled = a[2];
		RemoveEnabled = a[3];
		
		addMouseListener(ClickedLocation);
		
		FlipButton.setBounds(210,400,80,30);
		FlipButton.setToolTipText("Start game");
		FlipButton.addActionListener(FlipPieces);
		
		if (FlipEnabled == 1) {
			add(FlipButton);
		} else {
			remove(FlipButton);
		}
		
		Init();
	}
	
	// Callback function for FlipButton
	ActionListener FlipPieces = new ActionListener(){
		public void actionPerformed(ActionEvent event){
			_board.flipPieces();
			repaint();
			CompTurn();
		}
	};
	
	private void Init(){
		// not used yet
	}
	
	public void paintComponent(Graphics g){
		clear(g);
		g2d = (Graphics2D)g;
		
		// Initialize variable here after panel is rendered
		
		// color for board
		g2d.setColor(Color.ORANGE);
		
		//ensure board is only created once
		if(!BoardCreated){
			_board = new Board(this.getWidth(), this.getHeight(), rows, cols);
			BoardCreated = true;
		}
		
		// draws the board and pieces and count used spots
		for(int i=0; i<cols; i++){
			for(int j=0; j<rows; j++){
				g2d.fill(_board.getTile(i, j));	// draws board section
				if(_board.getTile(i, j).GetPlayer() == Player.ONE){	// fills in color of piece
					g2d.setColor(Color.RED);
					g2d.fill(_board.getTile(i, j).Center);
				} else if(_board.getTile(i, j).GetPlayer() == Player.TWO){
					g2d.setColor(Color.BLACK);
					g2d.fill(_board.getTile(i, j).Center);
				}
				g2d.setColor(Color.ORANGE);
			}
		}
	}
	
	protected void clear(Graphics g){
		super.paintComponent(g);
	}
		
	public void CompTurn(){
		Error error = _board.AIMove();
		if (error != Error.SUCCESS) {
			
		}
		repaint();
		if (checkGameStateWin()) {
			return;
		}		
	}
	
	MouseListener ClickedLocation = new MouseListener() {
		public void mouseClicked(MouseEvent e) {}

		public void mouseEntered(MouseEvent e) {}

		public void mouseExited(MouseEvent e) {}

		public void mousePressed(MouseEvent e) {}

		public void mouseReleased(MouseEvent e) {
			Point p = e.getPoint();
			Error error = Error.SUCCESS;
			if (_board.contains(p)) {
				Tile tile = _board.getTile(p);
				if (RemoveEnabled == 1 && tile._row == 0) {
					error = _board.removePiece(tile._column, Player.ONE);
				}
				else {
					error = _board.addPiece(tile._column, Player.ONE);
				}
				if (error != Error.SUCCESS) {
					if (error == Error.WRONG_PLAYER) {
						return;
					}
					// handle error
				}
				repaint();
				if (checkGameStateWin()) {
					return;
				} else {
					CompTurn();
				}
			} else {

				//JOptionPane.showMessageDialog(null,"ERROR: Must click on board");
			
			}
		}

		
	};
	
	boolean checkGameStateWin() {
		switch(_board.getGameState()) {
		case PLAYER_1_WIN:
			JOptionPane.showMessageDialog(null,"PLAYER 1 WON");
			NL.handleNotification(null, "You won the game!");
			BoardCreated = false;
			return true;
		case PLAYER_2_WIN:
			JOptionPane.showMessageDialog(null,"PLAYER 2 WON");
			BoardCreated = false;
			NL.handleNotification(null, "You loss the game!");
			return true;
		case DRAW:
			BoardCreated = false;
			NL.handleNotification(null, "Draw Game!!");
			return true;
		default:
			return false;
				
		}
	}
	
}
